package com.class3.team.fossilProject.Controller;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.util.ClassUtils;
import org.springframework.web.bind.annotation.*;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.class3.team.fossilProject.service.BookService;

@RestController
@RequestMapping("/distance")
public class DistanceController {
	
	
	@GetMapping("/{upValue}")
	@ResponseBody
	public JSONArray get(@PathVariable(name = "upValue")  double upValue ,@RequestParam(name = "time") String time) throws IOException{
		
		String [] r = new String[3];
		r[0] = "res/earlyT/result.xlsx";
		r[1] = "res/midT/result.xlsx";
		r[2] = "res/lateT/result.xlsx";

		System.out.println("time   "+time);

		JSONArray result = new JSONArray();
		
		
		int [] num = new int[3];
		num[0] = 61;
//		num[1] = 61;
//		num[2] = 61;
		
		num[1] = 91;
		num[2] = 990;
		
		System.out.println(upValue);
		int i;
		if(time.equals("Late Triassic"))
			 i=2;
		else if(time.equals("Middle Triassic"))
			i=1;
		else i=0;

			/*
			Resource res = new ClassPathResource(r[i]);
			if(res.exists()) {
				System.out.println("成功获取到数据！");
			}
			else {
				System.out.println("获取数据失败");
			}
			//InputStream in = this.getClass().getResourceAsStream(r[i]);	
			//InputStream in = res.getInputStream();
			*/
		ClassPathResource c = new ClassPathResource(r[i]);
		InputStream in = c.getInputStream();
		XSSFWorkbook wb = null;

		wb = new XSSFWorkbook(in);

		XSSFSheet ansSheet = wb.getSheetAt(0);
		XSSFSheet locSheet = wb.getSheetAt(1);

		JSONArray array = func1(ansSheet, locSheet, num[i], upValue);

		for(int j = 0; j< array.size();j++) {
			result.add(array.getJSONObject(j));
			//System.out.println(array.getJSONObject(j));
		}

		wb.close();
		
		
		
		return result;
	}
	
	// sheet表示处理的表，num表示这个表的行数和列数（二者相等）
	@SuppressWarnings("deprecation")
	public JSONArray func1(XSSFSheet ans,XSSFSheet loc, int num, double upValue){
		JSONArray res = new JSONArray();
		
		
		
		for(int i = 0;i < num;i++){
			for(int j = 0; j < num;j++){
				//得到点i和点j之间的距离值
				JSONObject temp = new JSONObject(true);
				JSONArray tempArray = new JSONArray();

				double d = ans.getRow(i).getCell(j).getNumericCellValue();
				if(d < upValue && d > 0) {
//					System.out.println("第 " + i + " 行，第 "+j	+" 列的值为："+d);
					
					double lon1 = loc.getRow(i).getCell(0).getNumericCellValue();
					double dim1 = loc.getRow(i).getCell(1).getNumericCellValue();
					double lon2 = loc.getRow(j).getCell(0).getNumericCellValue();
					double dim2 = loc.getRow(j).getCell(1).getNumericCellValue();
					
//					System.out.println("第 " + i + " 行，第  0 "+" 列的值为："+lon1 + ";第 " + i + " 行，第  1 "+" 列的值为："+dim1);
//					System.out.println("第 " + j + " 行，第  0 "+" 列的值为："+lon2 + ";第 " + j + " 行，第  1 "+" 列的值为："+dim2);

					double[] d1 = new double[2];
					double[] d2 = new double[2];
					d1[0] = d1[1]  = 0;
					d2[0] = d2[1]  = 0;

					d1[1] = lon1;d1[0] = dim1;
					
					d2[1] = lon2;d2[0] = dim2;
					
					tempArray.add(d1);
					tempArray.add(d2);
					
					
					temp.put("polylineData", tempArray);
					temp.put("width", 1.0/d);
					
					res.add(temp);
				
				}
			}
		}
		return res;
		
	}

}
